package com.zhang.zxx.boot.common.base;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhang.zxx.boot.common.enums.MsgEunm;
import com.zhang.zxx.boot.common.exception.BusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.validation.Valid;
import java.util.Objects;


/**
 * Request:请求参数实体
 *
 * @author zhangxiaoxiang
 * @date 2021年11月27日
 */
@Data
@EqualsAndHashCode
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("Request请求参数实体")
public class Request<A> {

    /**
     * 业务参数
     */
    @JsonAlias("param")
    @Valid
    @ApiModelProperty(value = "业务参数")
    private A data;

    public static <T> Request<T> build() {
        return new Request<T>();
    }

    public static <T> Request<T> build(T param) {
        return Request.<T>build().setData(param);
    }

    @JsonIgnore
    public A getSec() {
        if (Objects.isNull(data)) {
            throw new BusinessException(MsgEunm.PARAM_ERROR);
        }
        return data;
    }

    @JsonIgnore
    public A get() {
        return data;
    }
}
